<template>
	<Modal title="添加" :value="value" :mask-closable="true" :footer-hide="true" @input="$emit('input', $event)">
		<Form ref="formLkyMajor" :model="formLkyMajor" :rules="ruleValidate" label-position="right" :label-width="80">
			<FormItem label="所属学院" prop="name1">
				<Select v-model="formLkyMajor.coid" style="width:200px" @on-open-change="onOpenChange">
					<Option v-for="item in collegeList" :value="item.id" :key="item.id">
						{{ item.name }}
					</Option>
				</Select>
			</FormItem>
			<FormItem label="专业名称" prop="name">
				<Input type="text" v-model="formLkyMajor.name" placeholder="专业名称" />
			</FormItem>
			<FormItem label="专业编号" prop="major_number">
				<Input type="text" v-model="formLkyMajor.major_number" placeholder="专业编号" />
			</FormItem>
			<FormItem label="备注信息" prop="remark_info">
				<Input type="text" v-model="formLkyMajor.remark_info" placeholder="备注信息" />
			</FormItem>
			<FormItem>
				<Button type="primary" @click="handleSubmit('formLkyMajor')">提交</Button>
				<Button @click="handleReset('formLkyMajor')" style="margin-left: 8px">重置</Button>
			</FormItem>
		</Form>
	</Modal>
</template>
<script>
	import {
		save
	} from '@/api/lky/lkyMajor'
	import {
		queryAll
	} from '@/api/lky/lkyCollege'
	import {
		createModelObj
	} from '@/libs/util'

	export default {
		name: 'Add',
		props: {
			value: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				collegeList: [],
				formLkyMajor: {
					coid: '', // 学院id
					name: '',
					major_number: '',
					remark_info: ''
				},
				ruleValidate: {
         
					name: [{
							required: true,
							message: '该值不能为空',
							trigger: 'blur'
						},
						{
							type: 'string',
							max: 20,
							message: '长度最长为20',
							trigger: 'blur'
						}
					],
					major_number: [{
							required: true,
							message: '该值不能为空',
							trigger: 'blur'
						},
						{
							type: 'string',
							max: 20,
							message: '长度最长为20',
							trigger: 'blur'
						}
					],
					remark_info: [{
							required: false,
							message: '该值不能为空',
							trigger: 'blur'
						},
						{
							type: 'string',
							max: 200,
							message: '长度最长为200',
							trigger: 'blur'
						}
					]
				}
			}
		},
		methods: {
			onOpenChange() {
				queryAll().then(res => {
					console.log(res)
					this.collegeList = res.data
				})
			},
			handleSubmit(name) {
				this.$refs[name].validate(valid => {
					if (valid) {
						save(Object.assign({}, createModelObj(this.formLkyMajor, 'lkyMajor'))).then(
							res => {
								if (res) {
									this.$emit('addRow', res.row)
									this.$emit('input', false)
									this.handleReset(name)
								}
							}
						)
					} else {
						this.$Message.error('验证失败!')
					}
				})
			},
			handleReset(name) {
				this.$refs[name].resetFields()
			}
		}
	}
</script>
